package com.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Map;

@SpringBootApplication
@RestController
public class LoadBalancerWebApplication {
    private final WebClient.Builder loadBalancedWebClientBuilder;
    private final ReactorLoadBalancerExchangeFilterFunction lbFunction;

    public LoadBalancerWebApplication(WebClient.Builder webClientBuilder,
                                                 ReactorLoadBalancerExchangeFilterFunction lbFunction) {
        this.loadBalancedWebClientBuilder = webClientBuilder;
        this.lbFunction = lbFunction;
    }

    public static void main(String[] args) {
        SpringApplication.run(LoadBalancerWebApplication.class, args);
    }

    @GetMapping("/hello")
    public Mono<String> hello() {
        return WebClient.builder().filter(lbFunction).build().get()
                .uri("http://say-hello/hello").retrieve().bodyToMono(String.class);
    }

    @GetMapping("/goodbye")
    public Mono<String> goodbye() {
        return WebClient.builder().filter(lbFunction).build().get()
                .uri("http://say-goodbye/goodbye").retrieve().bodyToMono(String.class);
    }
}
